added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CSSparseFile / NativeMethod.cs
blob77929e39880c6b277813ff51526604337ca2309a
1 /****************************** Module Header ******************************\
2 * Module Name: NativeMethod.cs
3 * Project: CSSparseFile
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Declarations of native structs and functions for operations on sparse file.
7 *
8 * This source is subject to the Microsoft Public License.
9 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 * All other rights reserved.
12 * History:
13 * * 7/5/2009 11:42 AM Jialiang Ge Created
14 \***************************************************************************/
16 #region Using directives
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21 using System.Runtime.InteropServices;
22 using System.Threading;
23 using Microsoft.Win32.SafeHandles;
24 using System.IO;
25 #endregion
28 [StructLayout(LayoutKind.Sequential)]
29 public struct FILE_ZERO_DATA_INFORMATION
31 public Int64 FileOffset;
32 public Int64 BeyondFinalZero;
35 [StructLayout(LayoutKind.Sequential)]
36 public struct FILE_ALLOCATED_RANGE_BUFFER
38 public Int64 FileOffset;
39 public Int64 Length;
42 [Flags]
43 public enum FileSystemFeature : uint
45 CaseSensitiveSearch = 1,
46 CasePreservedNames = 2,
47 UnicodeOnDisk = 4,
48 PersistentACLS = 8,
49 FileCompression = 0x10,
50 VolumeQuotas = 0x20,
51 SupportsSparseFiles = 0x40,
52 SupportsReparsePoints = 0x80,
53 VolumeIsCompressed = 0x8000,
54 SupportsObjectIDs = 0x10000,
55 SupportsEncryption = 0x20000,
56 NamedStreams = 0x40000,
57 ReadOnlyVolume = 0x80000,
58 SequentialWriteOnce = 0x100000,
59 SupportsTransactions = 0x200000,
62 [Flags]
63 public enum EIoControlCode : uint
65 ChangerBase = 0x30,
66 ChangerExchangeMedium = 0x304020,
67 ChangerGetElementStatus = 0x30c014,
68 ChangerGetParameters = 0x304000,
69 ChangerGetProductData = 0x304008,
70 ChangerGetStatus = 0x304004,
71 ChangerInitializeElementStatus = 0x304018,
72 ChangerMoveMedium = 0x304024,
73 ChangerQueryVolumeTags = 0x30c02c,
74 ChangerReinitializeTarget = 0x304028,
75 ChangerSetAccess = 0x30c010,
76 ChangerSetPosition = 0x30401c,
77 DiskBase = 7,
78 DiskCheckVerify = 0x74800,
79 DiskControllerNumber = 0x70044,
80 DiskDeleteDriveLayout = 0x7c100,
81 DiskEjectMedia = 0x74808,
82 DiskFindNewDevices = 0x74818,
83 DiskFormatDrive = 0x7c3cc,
84 DiskFormatTracks = 0x7c018,
85 DiskFormatTracksEx = 0x7c02c,
86 DiskGetCacheInformation = 0x740d4,
87 DiskGetDriveGeometry = 0x70000,
88 DiskGetDriveLayout = 0x7400c,
89 DiskGetMediaTypes = 0x70c00,
90 DiskGetPartitionInfo = 0x74004,
91 DiskGrowPartition = 0x7c0d0,
92 DiskHistogramData = 0x70034,
93 DiskHistogramReset = 0x70038,
94 DiskHistogramStructure = 0x70030,
95 DiskIsWritable = 0x70024,
96 DiskLoadMedia = 0x7480c,
97 DiskLogging = 0x70028,
98 DiskMediaRemoval = 0x74804,
99 DiskPerformance = 0x70020,
100 DiskReassignBlocks = 0x7c01c,
101 DiskRelease = 0x74814,
102 DiskRequestData = 0x70040,
103 DiskRequestStructure = 0x7003c,
104 DiskReserve = 0x74810,
105 DiskSenseDevice = 0x703e0,
106 DiskSetCacheInformation = 0x7c0d8,
107 DiskSetDriveLayout = 0x7c010,
108 DiskSetPartitionInfo = 0x7c008,
109 DiskSmartGetVersion = 0x74080,
110 DiskSmartRcvDriveData = 0x7c088,
111 DiskSmartSendDriveCommand = 0x7c084,
112 DiskUpdateDriveSize = 0x7c0c8,
113 DiskVerify = 0x70014,
114 FsctlAllowExtendedDasdIo = 0x90083,
115 FsctlCreateOrGetObjectId = 0x900c0,
116 FsctlCreateUsnJournal = 0x900e7,
117 FsctlDeleteObjectId = 0x900a0,
118 FsctlDeleteReparsePoint = 0x900ac,
119 FsctlDeleteUsnJournal = 0x900f8,
120 FsctlDismountVolume = 0x90020,
121 FsctlDumpPropertyData = 0x90097,
122 FsctlEnableUpgrade = 0x980d0,
123 FsctlEncryptionFsctlIo = 0x900db,
124 FsctlEnumUsnData = 0x900b3,
125 FsctlExtendVolume = 0x900f0,
126 FsctlFileSystemGetStatistics = 0x90060,
127 FsctlFindFilesBySid = 0x9008f,
128 FsctlGetCompression = 0x9003c,
129 FsctlGetHfsInformation = 0x9007c,
130 FsctlGetNtfsFileRecord = 0x90068,
131 FsctlGetNtfsVolumeData = 0x90064,
132 FsctlGetObjectId = 0x9009c,
133 FsctlGetReparsePoint = 0x900a8,
134 FsctlGetRetrievalPointers = 0x90073,
135 FsctlGetVolumeBitmap = 0x9006f,
136 FsctlHsmData = 0x9c113,
137 FsctlHsmMsg = 0x9c108,
138 FsctlInvalidateVolumes = 0x90054,
139 FsctlIsPathnameValid = 0x9002c,
140 FsctlIsVolumeDirty = 0x90078,
141 FsctlIsVolumeMounted = 0x90028,
142 FsctlLockVolume = 0x90018,
143 FsctlMarkAsSystemHive = 0x9004f,
144 FsctlMarkHandle = 0x900fc,
145 FsctlMarkVolumeDirty = 0x90030,
146 FsctlMoveFile = 0x90074,
147 FsctlNssControl = 0x9810c,
148 FsctlNssRcontrol = 0x94118,
149 FsctlOpBatchAckClosePending = 0x90010,
150 FsctlOplockBreakAckNo2 = 0x90050,
151 FsctlOplockBreakAcknowledge = 0x9000c,
152 FsctlOplockBreakNotify = 0x90014,
153 FsctlQueryAllocatedRanges = 0x940cf,
154 FsctlQueryFatBpb = 0x90058,
155 FsctlQueryRetrievalPointers = 0x9003b,
156 FsctlQueryUsnJournal = 0x900f4,
157 FsctlReadFileUsnData = 0x900eb,
158 FsctlReadPropertyData = 0x90087,
159 FsctlReadRawEncrypted = 0x900e3,
160 FsctlReadUsnJournal = 0x900bb,
161 FsctlRecallFile = 0x90117,
162 FsctlRequestBatchOplock = 0x90008,
163 FsctlRequestFilterOplock = 0x9005c,
164 FsctlRequestOplockLevel1 = 0x90000,
165 FsctlRequestOplockLevel2 = 0x90004,
166 FsctlSecurityIdCheck = 0x940b7,
167 FsctlSetCompression = 0x9c040,
168 FsctlSetEncryption = 0x900d7,
169 FsctlSetObjectId = 0x90098,
170 FsctlSetObjectIdExtended = 0x900bc,
171 FsctlSetReparsePoint = 0x900a4,
172 FsctlSetSparse = 0x900c4,
173 FsctlSetZeroData = 0x980c8,
174 FsctlSisCopyFile = 0x90100,
175 FsctlSisLinkFiles = 0x9c104,
176 FsctlUnlockVolume = 0x9001c,
177 FsctlWritePropertyData = 0x9008b,
178 FsctlWriteRawEncrypted = 0x900df,
179 FsctlWriteUsnCloseRecord = 0x900ef,
180 StorageBase = 0x2d,
181 StorageCheckVerify = 0x2d4800,
182 StorageCheckVerify2 = 0x2d0800,
183 StorageEjectionControl = 0x2d0940,
184 StorageEjectMedia = 0x2d4808,
185 StorageFindNewDevices = 0x2d4818,
186 StorageGetDeviceNumber = 0x2d1080,
187 StorageGetMediaTypes = 0x2d0c00,
188 StorageGetMediaTypesEx = 0x2d0c04,
189 StorageLoadMedia = 0x2d480c,
190 StorageLoadMedia2 = 0x2d080c,
191 StorageMcnControl = 0x2d0944,
192 StorageMediaRemoval = 0x2d4804,
193 StorageObsoleteResetBus = 0x2dd000,
194 StorageObsoleteResetDevice = 0x2dd004,
195 StoragePredictFailure = 0x2d1100,
196 StorageRelease = 0x2d4814,
197 StorageReserve = 0x2d4810,
198 StorageResetBus = 0x2d5000,
199 StorageResetDevice = 0x2d5004,
200 VideoQueryDisplayBrightness = 0x230498,
201 VideoQuerySupportedBrightness = 0x230494,
202 VideoSetDisplayBrightness = 0x23049c
205 /// <summary>
206 /// Native methods
207 /// </summary>
208 internal class NativeMethod
210 public const int MAX_PATH = 260;
211 public const int ERROR_MORE_DATA = 234;
213 /// <summary>
214 /// Sends a control code directly to a specified device driver, causing
215 /// the corresponding device to perform the corresponding operation.
216 /// </summary>
217 /// <param name="Device">
218 /// A handle to the device on which the operation is to be performed.
219 /// </param>
220 /// <param name="IoControlCode">
221 /// The control code for the operation.
222 /// </param>
223 /// <param name="InBuffer">
224 /// A pointer to the input buffer that contains the data required to
225 /// perform the operation. This parameter can be NULL if dwIoControlCode
226 /// specifies an operation that does not require input data.
227 /// </param>
228 /// <param name="InBufferSize">
229 /// The size of the input buffer, in bytes.
230 /// </param>
231 /// <param name="OutBuffer">
232 /// A pointer to the output buffer that is to receive the data returned
233 /// by the operation. This parameter can be NULL if dwIoControlCode
234 /// specifies an operation that does not return data.
235 /// </param>
236 /// <param name="OutBufferSize">
237 /// The size of the output buffer, in bytes.
238 /// </param>
239 /// <param name="BytesReturned">
240 /// A pointer to a variable that receives the size of the data stored in
241 /// the output buffer, in bytes.
242 /// </param>
243 /// <param name="Overlapped">
244 /// A pointer to an OVERLAPPED structure.
245 /// </param>
246 /// <returns></returns>
247 [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
248 public static extern bool DeviceIoControl(
249 SafeFileHandle Device,
250 EIoControlCode IoControlCode,
251 IntPtr InBuffer,
252 int InBufferSize,
253 IntPtr OutBuffer,
254 int OutBufferSize,
255 ref int BytesReturned,
256 [In] ref NativeOverlapped Overlapped
259 /// <summary>
260 /// Retrieves information about the file system and volume associated
261 /// with the specified root directory.
262 /// </summary>
263 /// <param name="RootPathName">
264 /// A string that contains the root directory of the volume to be
265 /// described. For example, "C:\".
266 /// </param>
267 /// <param name="VolumeNameBuffer">
268 /// A pointer to a buffer that receives the name of a specified volume.
269 /// The maximum buffer size is MAX_PATH+1.
270 /// </param>
271 /// <param name="VolumeNameSize">
272 /// The length of a volume name buffer, in TCHARs. The maximum buffer
273 /// size is MAX_PATH+1.
274 /// </param>
275 /// <param name="VolumeSerialNumber">
276 /// A pointer to a variable that receives the volume serial number.
277 /// </param>
278 /// <param name="MaximumComponentLength">
279 /// A pointer to a variable that receives the maximum length, in TCHARs,
280 /// of a file name component that a specified file system supports.
281 /// </param>
282 /// <param name="FileSystemFlags">
283 /// A pointer to a variable that receives flags associated with the
284 /// specified file system.
285 /// </param>
286 /// <param name="FileSystemNameBuffer">
287 /// A pointer to a buffer that receives the name of the file system, for
288 /// example, the FAT file system or the NTFS file system. The maximum
289 /// buffer size is MAX_PATH+1.
290 /// </param>
291 /// <param name="FileSystemNameSize">
292 /// The length of the file system name buffer, in TCHARs. The maximum
293 /// buffer size is MAX_PATH+1.
294 /// </param>
295 /// <returns></returns>
296 [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
297 public static extern bool GetVolumeInformation(
298 string RootPathName,
299 StringBuilder VolumeNameBuffer,
300 int VolumeNameSize,
301 out uint VolumeSerialNumber,
302 out uint MaximumComponentLength,
303 out FileSystemFeature FileSystemFlags,
304 StringBuilder FileSystemNameBuffer,
305 int FileSystemNameSize
308 /// <summary>
309 /// Retrieves the actual number of bytes of disk storage used to store a
310 /// specified file. If the file is located on a volume that supports
311 /// compression and the file is compressed, the value obtained is the
312 /// compressed size of the specified file. If the file is located on a
313 /// volume that supports sparse files and the file is a sparse file, the
314 /// value obtained is the sparse size of the specified file.
315 /// </summary>
316 /// <param name="FileName">The name of the file.</param>
317 /// <param name="FileSizeHigh">
318 /// The high-order DWORD of the compressed file size.
319 /// </param>
320 /// <returns>
321 /// If the function succeeds, the return value is the low-order DWORD of
322 /// the actual number of bytes of disk storage used to store the
323 /// specified file, and if lpFileSizeHigh is non-NULL, the function puts
324 /// the high-order DWORD of that actual value into the DWORD pointed to
325 /// by that parameter. This is the compressed file size for compressed
326 /// files, the actual file size for noncompressed files.
327 /// </returns>
328 [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
329 public static extern uint GetCompressedFileSize(string FileName,
330 out uint FileSizeHigh);